export function deepParse(json: string): any {
  try {
    const parsedJson = JSON.parse(json)
    return parse(parsedJson)
  }
  catch (error) {
    console.error(error)
    return null
  }
}

function parse(value: any): any {
  for (const key in value) {
    if (Object.prototype.hasOwnProperty.call(value, key)) {
      try {
        const obj = JSON.parse(value[key])
        value[key] = obj
      }
      catch {}
    }
  }
  return value
}

export function deepStringify(obj: Record<string, any>): string {
  return JSON.stringify(stringify(obj))
}

function stringify(value: any): any {
  for (const key in value) {
    if (Object.prototype.hasOwnProperty.call(value, key)) {
      try {
        const json = JSON.stringify(value[key])
        value[key] = json
      }
      catch {}
    }
  }
  return value
}
